package com.diao.myhub.controller;

import com.diao.myhub.model.dto.GreatDTO;
import com.diao.myhub.enums.LikeTypeEnum;
import com.diao.myhub.model.po.Great;
import com.diao.myhub.model.po.User;
import com.diao.myhub.service.CommentService;
import com.diao.myhub.service.GreatService;
import com.diao.myhub.service.QuestionService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

//import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Morty
 */
@Controller
public class LikeController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private GreatService greatService;
    @Autowired
    private CommentService commentService;

    @RequestMapping(value = "/like", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(rollbackFor = RuntimeException.class)
    public String like(@RequestBody GreatDTO greatDTO, HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        // 如果用户不为空,记录或修改点赞对象和用户的关系
        if (user != null) {
            List<Great> record = greatService.getLikeRecord(user.getId(), greatDTO.getType(), greatDTO.getLikeId());
            Great great = new Great();
            BeanUtils.copyProperties(greatDTO, great);
            great.setUserId(user.getId());
            if (record == null || record.isEmpty()) {
                greatService.createRecord(great);
            } else {
                // 刷新数据库记录当中的点赞状态
                record.get(0).setStatus(greatDTO.getStatus());
                greatService.updateRecord(record.get(0));
            }
            // 刷新用户表和评论表点赞记录
            if (LikeTypeEnum.LIKE_TYPE_QUESTION.getType() == greatDTO.getType()) {
                questionService.refreshLikeCount(greatDTO.getLikeId());
            } else if (LikeTypeEnum.LIKE_TYPE_COMMENT.getType() == greatDTO.getType()) {
                commentService.refreshLikeCount(greatDTO.getLikeId());
            }
        }
        return "ok";
    }
}
